﻿$(function () {
    $("a").focus(function () { this.blur(); });
    $("input[type='button']").focus(function () { this.blur(); });

    $("#userName").blur(function () {
        check_userName();
    });

    $("#txtHoggeryManager").blur(function () {
        check_hoggeryManagerName();
    });

    $('#img').click(function () {
        changIdentifyCode();
    });

    $('#identity').blur(function () {
        check_code();
    });

//    $('#regsubmit').click(function () {
//        check_userName();
//        check_hoggeryManagerName();
//        valid_code_check = check_code();
//        if (userName_check && valid_code_check && hoggeryManager_check) {
//            reg();
//        }
//    });

});

function submitRegister() {
    //valid_code_check = check_code();
    if (check_userName() && pwConfirm() && check_code() && check_hoggeryManagerName()) {
        return true;
    }
    return false;
}


var valid_code_check = false;
function check_code() {
    var code = $('#identity').val();
    if (!code) {
        $('#tips_authcode').html('<span>验证码不能为空。</span>');
        return false;
    } 
    else {
        $('#tips_authcode').html('不区分大小写，点击即可更换一个');
        return true;
    }
}
//注册
var register_check = true;
function reg() {
    $("#regsubmit").unbind("click");
    $("#regstat").val('');
    var userName = $("#userName").val();
    var hoggeryManager = $('#txtHoggeryManager').val();
    var password = $('#password1').val();
    var validCode = $('#identity').val();
    var hoggeryName = $("#txtHoggeryName").val();

    var postUrl = $('#MainContent_hidden').val();
//    var registSpan = "<span id='regstat'>注册中...</span>";
//    $("#regist_msg > span").remove();
//    $("#regist_msg").append(registSpan);
    //$("#regstat").html('注册中...');
    $.ajax2({
        type: "post",
        contentType: "application/json",
        url: "../WebServices/LoginAndRegist.asmx/Regist",
        dataType: "json",
        data: "{ userName: '" + userName + "',password:'" + password + "',hoggeryManager:'" + hoggeryManager +
            "',hoggeryName:'" + hoggeryName + "',validCode:'" + validCode + "' }",
        success: function (data) {
            var span = "<span>" + data.d.Message + "</span>";
            if (data.d.Flag.toLocaleString() == 'ture') {
                register_check = true;
            } else {
                register_check = false;
            }
            switch (data.d.MessageId) {
                case "UserName":
                    $("#tips_userName").html('');
                    $("#tips_userName").append(span);
                    register_check = false;
                    break;
                case 'Password':
                    $("#tips_password").html('');
                    $("#tips_password").append(span);
                    register_check = false;
                    break;
                case 'ValidateCode':
                    $("#tips_authcode").html('');
                    $("#tips_authcode").append(span);
                    register_check = false;
                    break;
                case 'Email':
                    $("#tips_email").html('');
                    $("#tips_email").append(span);
                    register_check = false;
                    break;
                case 'Unknown':
                    registSpan = "<span id='regstat' style='color: #CC0000'>" + data.d.Message + "</span>";
                    $("#regist_msg > span").remove();
                    $("#regist_msg").append(registSpan);
                    register_check = false;
                    break;
            }

            changIdentifyCode();
            if (register_check) {
                cleanText();
                window.location.href = "../webs/SowManagement.aspx";
            }
            // successRegist(register_check);
            // postRedirect(register_check);
        }
    });
    $('#regsubmit').bind('click', reg);
}

var userName_check = false;
function check_userName() {
    var userName = $("#userName").val();
    var isValidName = checkNickname(userName);
    var str = '<div class="wt01" id="tips_userName"></div>';
    switch (isValidName) {
        case 0:
            str = '<div class="wt01" id="tips_userName"><span>用户名不能为空。</span></div>'; break;
        case 2:
            str = '<div class="wt01" id="tips_userName"><span>用户名长度不能超过31位。</span></div>'; break;
        case 1:
            $.ajax2({
                type: "post",
                contentType: "application/json",
                url: "../WebServices/LoginAndRegist.asmx/IsExsitedUserName",
                dataType: "json",
                data: "{ userName: '" + userName + "' }",
                success: function (data) {
                    if (data.d == "1") {
                        str = '<div class="wt01" id="tips_userName"><span>该用户名已经存在，请重新输入。</span></div>';
                        $("#userName").focus();
                        userName_check = false;
                    } else if (data.d == "2") {
                        str = '<div class="wt01" id="tips_userName"><span>用户名不能为空。</span></div>';
                        userName_check = false;
                    } else {
                        str = "";
                        userName_check = true;
                    }
                    $("#tips_userName").html(str);
                },
                error: function (error) {
                    alert("抱歉,服务器错误，我们会尽快修复。");
                }
            }); break;
    }
    $("#tips_userName").replaceWith(str);
    return userName_check;
}
var hoggeryManager_check = false;
function check_hoggeryManagerName() {
    var name = $('#txtHoggeryManager').val();
    var str = '<div class="wt01" id="tips_Manager"></div>';
    $("#tips_Manager").replaceWith(str);
    var isValidName = checkNickname(name);
    if (isValidName == 0) {
        str = '<div class="wt01" id="tips_Manager"><span>厂长姓名不能为空。</span></div>';
        hoggeryManager_check = false;
    }
    if (isValidName == 1) {
        hoggeryManager_check = true;
    }
    if (isValidName == 2) {
        str = '<div class="wt01" id="tips_Manager"><span>姓名长度不能超过31位。</span></div>';
        hoggeryManager_check = false;
    }
    $("#tips_Manager").replaceWith(str);
    return hoggeryManager_check;
}

//显示颜色
function pwStrength(pwd, targetid) {
    var strength = 0;
    var str = string = '';
    if (pwd == null || pwd == '') {
        strength = -1; str = '密码不能为空。';
    } else {
        S_level = checkStrong(pwd);
        switch (S_level) {
            case 0:
                strength = 0; str = '密码长度应在6～16个字符。'; break;
            case 1:
                strength = 1; str = '弱<span style="margin-left:12px;">试试字母、数字、标点组合</span>'; break;
            case 2:
                strength = 2; str = '中'; break;
            default:
                strength = 3; str = '强'; break;
        }
    }
    if (typeof (targetid) == 'undefined') {
        targetid = 'tips_password';
    }
    if (strength > 0) {
        if (targetid == 'tips_password1') {
            string = '<div id="' + targetid + '" class="wt01"><span></span></div>';
        } else {
            string = '<div id="' + targetid + '" class="wt02"><div class="pass_str">密码强度：' + str + '</div><div class="strength str_bg0' + strength + '"></div></div>';
        }
    } else {
        string = '<div id="' + targetid + '" class="wt01"><span>' + str + '</span></div>';
    }
    $('#' + targetid).replaceWith(string);
    if (strength <= 0) {
        return false;
    }
    else {
        return true;
    }
}
/*
* 验证两次输入的密码是否一致
*/
function pwConfirm() {
    var flag = false;
    var pwd1 = $('#password1').val();
    var pwd2 = $('#password2').val();
    if (pwd1 != '' && pwd2 != '' && pwd1 != pwd2) {
        str = '<div id="tips_password1" class="wt01"><span>两次输入的密码不一致，请重新输入。</span></div>';
        $('#tips_password1').replaceWith(str);
    } else {
        flag = true;
    }
    return flag;
}


function successRegist(registCheck) {
    if (registCheck) {
        //        var count = postUrl.lastIndexOf('/');
        //        var pageName = postUrl.substring(count + 1, postUrl.length);
        //        alert(pageName);
        //        if (pageName.toLowerCase() != "Register.aspx".toLowerCase()) {
        //            window.location.href = postUrl;
        //        }
        var registSpan = "<span id='regstat'>注册成功，欢迎使用易养猪在线养猪管理系统。</span>";
        $("#regist_msg > span").remove();
        $("#regist_msg").append(registSpan);

    }
}

var cTime = 3;
function timeClose() {
    window.setTimeout('timeClose()', 1000);
    if (cTime <= 0) {
        window.location.href = "Account/Login.aspx";
    } else {
        $("#lbSecond").text(cTime);
        cTime--;
    }
}


